<?php defined('SYSPATH') OR die('No direct access allowed.');

class Page_Model extends ORM_Tree{

 public $parent_column='parent_id';
 
 protected $has_many = array('content','pagecont');
 protected $has_one = array('pagetemplate');
 protected $children = 'pages'; 
 protected $sorting = array('position' => 'asc');
 
 
 //public $metainfo;
 
	public function unique_key($id = NULL){
		if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id) ){
			return 'shortname';
		}
		return parent::unique_key($id);
	}
	
	public function shortname_check($shortname){
		if($shortname==''){
			return false;		
		}
		$parray=ORM::factory('page')->where(array('shortname'=>$shortname, 'id!='=>$this->id))->find_all()->as_array();
		if(count($parray)>0){
			return false;
		}
		return true;
	}

	public function get_contentmodule_list($container, $position=1000, $contdir='ext/'){
		$retarray=array();		
		$contmodules=$this->pagetemplate->get_container_modules_array($container);
		foreach($contmodules as $contmod){
			$retarray[]=array('title'=>$contmod->title, 'link'=>$contdir.'content/form?parent_id='.$this->id.'&position='.$position.'&contmod_id='.$contmod->id.'&parent_column=page_id&container='.$container, $contmod->title);
			
		}
		return $retarray;
	}

	public function get_ancestors(){
		$rarray=array();
		$pid=$this->parent_id;		
		while($pid!='0'){	
			$parent=ORM::factory('page', $pid);
			$pid=$parent->parent_id;
			$rarray[]=$parent;
		}
		$rarray=array_reverse($rarray);
		return $rarray;
	}	
	
	public function get_openones(){	
		$rarray=array($this->id);
		$pid=$this->parent_id;		
		
		while($pid!='0'){	
			$parent=ORM::factory('page', $pid);
			$rarray[]=$parent->id;		
			$pid=$parent->parent_id;
			
		}
		return $rarray;
	}
	
	
	
	
	
/////////////////////////////////////////// GET A PULLDOWN LIST FOR THE ACTION FORM OF THE CONTENTS (COPYTO)
	
	public function get_pulldown_list_for_contents(){	
		$allpages=ORM::factory('page');
		$pages=$allpages->orderby(array('parent_id'=>'', 'position'=>''))->find_all()->as_array();
		$list=array();
		$list+=$this->get_nested_pulldown_list_for_contents($pages,'0', '1');
		return $list;
	}
	
	public function get_nested_pulldown_list_for_contents ($pages, $parent_id, $step){
		$array=array();
		foreach($pages as $onepage){			
			if($onepage->parent_id==$parent_id){
				$str='';
				for($i=0;$i<$step;$i++){
					$str.='&nbsp;&nbsp;&nbsp;&nbsp;';
				}
				
				$array[$onepage->id]=$str.$onepage->title;
				$array+=$this->get_nested_pulldown_list_for_contents($pages, $onepage->id, $step+1);			
				
			}			
		}
		return $array;
	}

	
	
	
	/////////////////////////////////////////// GET A PULLDOWN LIST FOR THE ACTION FORM OF THE PAGE (MOVETO)
	
	public function get_pulldown_list(){
		$allpages=ORM::factory('page');
		$pages=$allpages->orderby(array('parent_id'=>'', 'position'=>''))->find_all()->as_array();
		
		$list=array('0'=>'root');
		$list+=$this->get_nested_pulldown_list($pages,'0', '1');
		
		return $list;
	}
	
	public function get_nested_pulldown_list ($pages, $parent_id, $step){
		$array=array();
		foreach($pages as $onepage){			
			if($onepage->parent_id==$parent_id){
				$str='';
				for($i=0;$i<$step;$i++){
					$str.='&nbsp;&nbsp;&nbsp;&nbsp;';
				}
				if($onepage->id!=$this->id){
					$array[$onepage->id]=$str.$onepage->title;
					$array+=$this->get_nested_pulldown_list($pages, $onepage->id, $step+1);			
				}
			}			
		}
		return $array;
	}


	public function count_children(){
		return ORM::factory('page')->where('parent_id',$this->id)->find_all()->count();
	}

	public function get_meta_object($lang=LANG){
		$this->reload();
		$pcs=$this->where('lang',$lang)->pagecont->as_array();
		
		if(count($pcs)>0){
			return $pcs[0];
		}
		
		$newmodel=ORM::factory('pagecont');
		$newmodel->lang=$lang;
		$newmodel->menutitle=$this->title;
		$newmodel->windowtitle=$this->title;
		$newmodel->page_id=$this->id;
		$newmodel->save();
		
		return $newmodel;
	}
}



